;
(function () {
    function cart() {
        let userMsg = localStorage.getItem('userMsg') ? JSON.parse(localStorage.getItem('userMsg')) : [];
        let i = 0;
        let onoff = userMsg.some((val, idx) => {
            i = idx;
            return val.onoff === 1;
        })
        if (!onoff) {
            alert('没有登录不允许打开购物车');
            location.href = 'http://localhost/blShop-gulp/src/login.html';
        }
    }
    cart();

    class Cart {
        constructor() {
            this.url = "http://localhost/blShop-gulp/src/json/goodlists.json";
            this.tabCon = document.querySelector('tbody');
            this.del=document.getElementById("deleteAll");
            this.cA=document.querySelectorAll(".check-all");
            this.priceTotal=document.getElementById("priceTotal");
            this.type=0;

            this.load();
        }
        load() {
            ajaxGet(this.url, (res) => {
                this.res = JSON.parse((res));
                this.getData();
            });
        }
        addEvent(){
            let that=this;
            // 小计 添加商品
            this.tabCon.addEventListener('click', function (eve) {
                that.cO=document.querySelectorAll(".check-one");
                let e = eve || window.event;
                let tar = e.target || e.srcElement;
                if (tar.className === "add") {
                    // 数量
                    that.totalVal = tar.previousElementSibling.value;
                    that.totalVal++;
                    tar.previousElementSibling.value = that.totalVal;
                    // 单价
                    that.uPrice = tar.parentNode.previousElementSibling;
                    // 总价
                    that.tPrice = tar.parentNode.nextElementSibling;
                    that.tPrice.innerHTML = (that.totalVal * that.uPrice.innerHTML).toFixed(2);
                    that.gId = tar.parentNode.parentNode.getAttribute('index');
                    that.updateData(function (i) {
                        that.goodsMsg[i].num = that.totalVal;
                    });
                }
                // 减少商品数量
                if (tar.className === "reduce") {
                    that.totalVal = tar.nextElementSibling.value;
                    if (that.totalVal > 1) {
                        that.totalVal--;
                        tar.nextElementSibling.value = that.totalVal;
                        // 单价
                        that.uRPrice = tar.parentNode.previousElementSibling;
                        // 总价
                        that.tRPrice = tar.parentNode.nextElementSibling;
                        that.tRPrice.innerHTML = (that.totalVal * that.uRPrice.innerHTML).toFixed(2);
                        that.gId = tar.parentNode.parentNode.getAttribute('index');
                        that.updateData(function (i) {
                            that.goodsMsg[i].num = that.totalVal;
                        });
                    }
                }
                // 删除商品
                if(tar.className==="delete"){
                    that.gId=tar.parentNode.parentNode.getAttribute("index");
                    that.updateData(function(i){
                        that.goodsMsg.splice(i,1);
                    });
                    tar.parentNode.parentNode.remove();
                }
                //单选按钮
                if(tar.className.indexOf("check-one")!=-1){
                    for(let i=0;i<that.cO.length;i++){
                        if(!that.cO[i].checked){
                            that.cA[0].checked=false;
                            that.cA[1].checked=false;
                            break;
                        }else{
                            that.cA[0].checked=true;
                            that.cA[1].checked=true;
                        }
                    }
                }
            });
            this.cO=document.querySelectorAll(".check-one");

            // 全选按钮
            this.cA[0].onclick=function(){
                console.log(that.cO);
                that.cA[1].checked=this.checked;
                that.cO.forEach(ele=>{
                    ele.checked=this.checked;
                })
            }
            this.cA[1].onclick=function(){
                that.cA[0].checked=this.checked;
                that.cO.forEach(ele=>{
                    ele.checked=this.checked;
                })
            }
        }
        getData() {
            this.goodsMsg = localStorage.getItem("goodsMsg") ? JSON.parse(localStorage.getItem("goodsMsg")) : [];
            this.display();
        }
        display() {
            let str = "";
            if (this.goodsMsg.length > 0) {
                for (let i = 0; i < this.res.length; i++) {
                    for (let j = 0; j < this.goodsMsg.length; j++) {
                        if (this.res[i].id === this.goodsMsg[j].id) {
                            str += `<tr index="${this.res[i].id}">
                            <td class="checkbox">
                                <input class="check-one check" type="checkbox">
                            </td>
                            <td class="goods">
                                <img src="${this.res[i].url}" alt="" />
                                <span>${this.res[i].name}</span>
                            </td>
                            <td class="price">${this.res[i].price}</td>
                            <td class="count">
                                <span class="reduce">-</span>
                                <input class="count-input" type="text" value="${this.goodsMsg[j].num}" />
                                <span class="add">+</span>
                            </td>
                            <td class="subtotal">${(this.res[i].price*this.goodsMsg[j].num).toFixed(2)}</td>
                            <td class="operation">
                                <span class="delete">删除</span>
                            </td>
                        </tr>`
                        }
                    }
                }
                this.tabCon.innerHTML = str;
            } else {
                this.tabCon.innerHTML = '<h2>亲，请先去&nbsp;&nbsp;<a href="http://localhost/blShop-gulp/src/index.html">首页</a>&nbsp;&nbsp;购买商品奥</h2>';
            }
            this.addEvent();           
        }
        
        updateData(cb) {
            let i;
            this.goodsMsg.some((val, idx) => {
                i = idx;
                return val.id === this.gId;
            });
            cb(i);
            localStorage.setItem("goodsMsg", JSON.stringify(this.goodsMsg));
        }
    }
    new Cart();
})();